/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.unipar.oficina.converters;

import br.com.unipar.oficina.modelo.cadastros.Grupo;
import br.com.unipar.oficina.service.cadastros.GrupoService;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.springframework.web.jsf.FacesContextUtils;



/**
 *
 * @author Thiago Marques
 */
@FacesConverter(value="converterGrupo")
public class ConverterGrupo implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value != null && !value.equals("")) {
            GrupoService grupoService = (GrupoService) FacesContextUtils.getWebApplicationContext(context).getBean("grupoService");
            return grupoService.getByIdGrupo(Long.valueOf(value));
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Grupo) {
            Grupo grupo = (Grupo) value;
            return String.valueOf(grupo.getIdGrupo());
        }
        return "";
    }

}
